package org.yi.fc.entity;

import java.util.ArrayList;
import java.util.List;

import org.yi.fc.utils.SensitiveWordUtils;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Model;
import com.mysql.fabric.xmlrpc.base.Data;

/**
 * @author ilgqh
 * 文章评论实体
 */
public class FComment extends Model<FComment> {

	private static final Logger logger = Logger.getLogger(FComment.class);
	
	private static final long serialVersionUID = -7741920536888392262L;
	
	public static FComment dao = new FComment();
	
	/**
	 * 没有注册或者登录的用户进行评论时采用的名称
	 */
	private final String DEFAULT_USER_NAME = "游客";
	
	/**
	 * 保存评论
	 * @throws Exception
	 */
	public void saveFComment() throws Exception{
		this.set("crateTime", new Data());
		String sensitiveWord = SensitiveWordUtils.sensitiveWordCheck((String)this.get("comment"));
		if(sensitiveWord == null){
			logger.error(String.format("请不要在评论中夹带敏感词汇【%s】",sensitiveWord));
			throw new Exception(String.format("请不要在评论中夹带敏感词汇【%s】",sensitiveWord));
		}else{
			this.save();
		}
	}
	
	/**
	 * 根据用户名称获取该用户所有的评论
	 * @param name
	 * @return
	 */
	public List<FComment> getFCommentsByUserName(String name){
		ArrayList<FComment> list = new ArrayList<FComment>();
		
		if(Strings.isNullOrEmpty(name)){
			String errorMsg = "根据用户名称获取该用户所有的评论时，传入用户名称不能为空";
			
			NullPointerException npe = new NullPointerException(errorMsg);
			logger.error(errorMsg,npe);
			throw npe;
		}
		
		if(DEFAULT_USER_NAME.equals(name)){
			logger.info("不能通过【getFCommentsByUserName】方法获取所有的游客评论");
			return list;
		}
		
		return find("select * from f_comment where user_name  = ? ", name);
	}
	
	/**
	 * 根据文章ID获取所有的评论
	 * @param id			文章ID
	 * @return
	 */
	public List<FComment> getFcommentByContentId(Object id){
		Preconditions.checkNotNull(id," 根据文章id获取所有的评论时，id不能为空");
		return find("select * from f_comment where content_id = ?", id);
	}
	
	
}
